package cn.bugDance.domain.strategy.service.rule.tree.factory;

import cn.bugDance.domain.strategy.model.entity.RuleTreeEntity;
import cn.bugDance.domain.strategy.model.entity.RuleTreeFactorEntity;
import cn.bugDance.domain.strategy.model.entity.StrategyAwardEntity;
import cn.bugDance.domain.strategy.repository.IStrategyRepository;
import cn.bugDance.domain.strategy.service.rule.tree.IRuleTree;
import cn.bugDance.domain.strategy.service.rule.tree.engine.IRuleEngine;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 规则策略树工厂
 */
@Component
@Slf4j
public class RuleTreeFactory {

    private Map<String, IRuleTree> ruleTreeGroup;
    private IStrategyRepository strategyRepository;
    private IRuleEngine ruleEngine;

    public RuleTreeFactory(Map<String, IRuleTree> ruleTreeGroup, IStrategyRepository strategyRepository, IRuleEngine ruleEngine) {
        this.ruleTreeGroup = ruleTreeGroup;
        this.strategyRepository = strategyRepository;
        this.ruleEngine = ruleEngine;
    }



    public RuleTreeActionEntity openRuleTree(RuleTreeFactorEntity ruleTreeFactorEntity){
        log.info("【策略领域层-RuleTreeFactory】: 规则规则树引擎开启责任执行, strategyId: {}, awardId: {}", ruleTreeFactorEntity.getStrategyId(), ruleTreeFactorEntity.getAwardId());

        Long strategyId = ruleTreeFactorEntity.getStrategyId();
        Integer awardId = ruleTreeFactorEntity.getAwardId();
        StrategyAwardEntity strategyAwardEntity = strategyRepository.queryStrategyAwardByStrategyIdAndAwardId(strategyId, awardId);
        ruleTreeFactorEntity.setRuleModels(strategyAwardEntity.ruleModels());

        RuleTreeEntity ruleTreeEntity = ruleEngine.armory(strategyId);

        return ruleEngine.process(ruleTreeEntity, ruleTreeFactorEntity);
    }


    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @Data
    public static class RuleTreeActionEntity {
        private Integer award;
    }



}
